package cc.wsyw126.java.lambda;

import java.util.Arrays;
import java.util.List;

/**
 * Created by junpeng.yang on 16/8/24.
 */
public class JavaReduce {
    public static void main(String[] args) {
        // Applying 12% VAT on each purchase

        // Old way:
        List<Integer> costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
        double total = 0;
        for (Integer cost : costBeforeTax) {
            double price = cost + .12*cost;
            total = total + price;

        }
        System.out.println("Total : " + total);
        System.out.println("------------");
        // New way:
        double bill = costBeforeTax.stream().map((cost) -> cost + .12 * cost)
                .reduce((sum, cost) -> sum + cost)
                .get();
        System.out.println("Total : " + bill);
        System.out.println("------------");
        double billWithStartValue = costBeforeTax.stream().map((cost) -> cost + .12*cost)
                .reduce(.0,Double::sum);
        System.out.println("billWithStartValue = " + billWithStartValue);
    }
}
